package com.open.party.cloud.view.login

import android.os.Bundle
import android.view.View
import android.view.WindowManager
import cn.android.x.model.data.ResultInfo
import cn.sinothk.hussars.views.BaseActivity
import com.gyf.immersionbar.ImmersionBar
import com.open.party.cloud.R
import com.open.party.cloud.model.UserEntity
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.viewModel.BaseViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_login.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class LoginActivity : BaseActivity(), View.OnClickListener {

    private var viewModel: BaseViewModel? = null

    private fun initNoticeTitle() {
        // 标题处理
        ImmersionBar.with(this)
            .statusBarDarkFont(false)
            .keyboardEnable(false)
            .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
            .init()

//        backBtn.setOnClickListener { finish() }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        initNoticeTitle()

        initView()
        viewModel = BaseViewModel()
    }

    private fun initView() {
        accountEt.setText(LocalCache.getLoginAccount())
        XUtils.view().focusMoveToEnd(accountEt)

        loginBtn.setOnClickListener(this)
        // 找回密码
        registerUserBtn.setOnClickListener(this)
        forgetPwdBtn.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
//        ImmSolftManager(this).showKeyboard(false)

        when (v) {
            loginBtn -> {
                toLogin()
            }
//            loginStyle02Item -> {
////                loginDownTip01.visibility = View.GONE
////                loginDownTip02.visibility = View.VISIBLE
////
////                loginPwdView.visibility = View.GONE
////                loginSmsView.visibility = View.VISIBLE
//            }
//
//            loginBtn -> {
//            }
//            signUpBtn -> {
//                XUtils.intent().openActivity(this@LoginActivity, RegisterActivity::class.java)
//                    .start()
//            }
//
//            protocolItem -> {
//                CommWebDetailsActivity.start(this, "用户使用协议", ServerConfig.getProtocolUrl())
//            }
//
            registerUserBtn -> {
                XUtils.intent().openActivity(this@LoginActivity, Register2Activity::class.java)
                    .start()
            }
//
//            wxLoginBtn -> {
//                XUtils.toast().show("持续完善中 ...")
//            }

            forgetPwdBtn -> {
                XUtils.intent().openActivity(this@LoginActivity, ForgetPwdActivity::class.java)
                    .start()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    private fun toLogin() {
        XUtils.phone().showKeyboard(this,false)
        val accountStr = accountEt.text.toString()
        val passWordStr = passWordEt.text.toString()

        if (XUtils.string().isEmpty(accountStr)) {
            XUtils.toast().show("请输入账号")
            return
        }

        if (XUtils.string().isEmpty(passWordStr)) {
            XUtils.toast().show("请输入密码")
            return
        }

        if (!XUtils.net().isAvailable) {
            XUtils.toast().show("当前网络不可用")
            return
        }
        showLoadingDialog("正在登录")
        viewModel?.doLogin(accountStr, passWordStr)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun receiveStudentEventBus(resultInfo: ResultInfo<UserEntity>) {

        hideLoadingDialog()

        when (resultInfo.code) {
            ResultInfo.SUCCESS -> {
                LocalCache.setLoginUser(resultInfo.data)
                LocalCache.setAutoLogin(true)
                LocalCache.setPwd(passWordEt.text.toString())
                finish()
                XUtils.toast().show("登录成功")
//                XUtils.intent().openActivity(this@LoginActivity, MainActivity::class.java)
//                    .finish(true).start()
            }
            ResultInfo.TOKEN_OVERDUE -> {
                // 不存在
            }
            else -> {
                XUtils.toast().show(resultInfo.msg)
            }
        }
    }
}
